package com.sduept.nwld.dataserver.controller.dataview;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.cim.QuotaManager;
import com.sduept.nwld.dataserver.model.param.QuotaInfoParam;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import org.apache.commons.lang.StringUtils;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.List;

@Named
@ViewScoped
public class QuotaInfoController extends AbstractController{
	private static final long serialVersionUID = -8595906041489246732L;
	@Autowired
	private QuotaManager qm;
	
	private List<QuotaInfoParam> quotas = new ArrayList<QuotaInfoParam>();
	private TreeNode root;
	private TreeNode currentNode;
	private QuotaInfoParam currentInfo = new QuotaInfoParam();
	private QuotaInfoParam currentGroup = new QuotaInfoParam();
	
	@PostConstruct
	public void init(){
		initNodes();
	}

	public void initNodes(){
		root = new LibTreeNode();
		List<String>groupNames = qm.getGroups();
		for (String name : groupNames) {
			new DefaultTreeNode(name, root);
		}
	}
	
	public void preCreate(){
		currentGroup = new QuotaInfoParam();
	}
	
	public void precreateInfo(){
		currentInfo = new QuotaInfoParam();
		String name = (String) currentNode.getData();
		currentInfo.setGroupName(name);
	}
	
	public void save(){
		if (StringUtils.isEmpty(currentGroup.getId())) {
			currentGroup.setName(currentGroup.getGroupName());
			currentGroup = qm.saveOrUpdateInfo(currentGroup);
			new DefaultTreeNode(currentGroup.getGroupName(), root);			
		}else{
			String from = (String) currentNode.getData();
			String to = currentGroup.getGroupName();
			qm.updateGroupName(from,to);
			initNodes();
		}
	}

	public void saveInfo(){
		String name = (String) currentNode.getData();
		currentInfo.setGroupName(name);
		qm.saveOrUpdateInfo(currentInfo);
		quotas = qm.getQuotaInfosByGroupName(name);
	}
	
	public void deleteGroup(){
		String groupName = (String) currentNode.getData();
		qm.deleteGroup(groupName);
		initNodes();
		quotas.clear();
	}
	
	public void delete(){
		qm.deleteInfo(currentInfo);
		quotas.remove(currentInfo);
		initNodes();
		addSuccessMessage("删除成功！");
	}
	
	public void onNodeSelect(NodeSelectEvent event){
		currentNode = event.getTreeNode();
		String name = (String) currentNode.getData();
		quotas = qm.getQuotaInfosByGroupName(name);
		currentGroup = quotas.get(0);
	}
	
	public void rowSelect(SelectEvent event){
		currentInfo = (QuotaInfoParam) event.getObject();
	}
	
	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getCurrentNode() {
		return currentNode;
	}

	public void setCurrentNode(TreeNode currentNode) {
		this.currentNode = currentNode;
	}

	public QuotaInfoParam getCurrentInfo() {
		return currentInfo;
	}

	public void setCurrentInfo(QuotaInfoParam currentInfo) {
		this.currentInfo = currentInfo;
	}

	public List<QuotaInfoParam> getQuotas() {
		return quotas;
	}

	public void setQuotas(List<QuotaInfoParam> quotas) {
		this.quotas = quotas;
	}

	public QuotaInfoParam getCurrentGroup() {
		return currentGroup;
	}

	public void setCurrentGroup(QuotaInfoParam currentGroup) {
		this.currentGroup = currentGroup;
	}
	
}
